package com.webside.playbackstatistics.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webside.base.baseservice.impl.AbstractService;
import com.webside.playbackstatistics.config.PlayStatisticsConfig;
import com.webside.playbackstatistics.mapper.SeriesStatMapper;
import com.webside.playbackstatistics.model.AnaDailyStatEntity;
import com.webside.playbackstatistics.model.SeriesStatEntity;
import com.webside.playbackstatistics.service.SeriesStatService;
import com.webside.resource.model.ResourceEntity;
import com.webside.series.mapper.BdictionaryMapper;
import com.webside.series.model.Bdictionary;

@Service("SeriesStatService")
public class SeriesStatServiceImpl extends AbstractService<ResourceEntity, Long> implements SeriesStatService {
	@Autowired
	private SeriesStatMapper seriesStatMapper;
	@Autowired
	private BdictionaryMapper dictionaryMapper;
	// 这句必须要加上。不然会报空指针异常，因为在实际调用的时候不是BaseMapper调用，而是具体的mapper，这里为userMapper

	public void setBaseMapper() {
		super.setBaseMapper(seriesStatMapper);
	}

	@Override
	public AnaDailyStatEntity conditionCommentAvg(String seriesName) {
		// TODO Auto-generated method stub
		SeriesStatEntity seriesStat = seriesStatMapper.conditionCommentAvg(seriesName);
		AnaDailyStatEntity anaDailyStat = new AnaDailyStatEntity();
		if (null != seriesStat) {
			// 如果发布时间不为空就获取当前时间和发布时见得到中间的天数
			if (null != seriesStat.getPublishTime() && !seriesStat.getPublishTime().equals("")) {

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Date toDay = new Date();
				try {
					toDay = df.parse(df.format(toDay));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Date publishTime = seriesStat.getPublishTime();
				try {
					publishTime = df.parse(df.format(seriesStat.getPublishTime()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Calendar cal = Calendar.getInstance();
				cal.setTime(toDay);
				long timeNew = cal.getTimeInMillis();
				cal.setTime(publishTime);
				long timeOld = cal.getTimeInMillis();

				long between_days = (timeNew - timeOld) / (1000 * 3600 * 24);
				// 得到改影片上映的天数
				int day = Integer.parseInt(String.valueOf(between_days));

				if (0 != day) {
					// 平均每天点播总次数
					if (0 != seriesStat.getPlayCount() && null != seriesStat.getPlayCount()
							&& !seriesStat.getPlayCount().equals("")) {
						anaDailyStat.setPlayCount(seriesStat.getPlayCount() / day);
					} else {

						anaDailyStat.setPlayCount(0);
					}
					// 平均每天播放总时长
					if (0 != seriesStat.getPlayTimeSum() && null != seriesStat.getPlayTimeSum()
							&& !seriesStat.getPlayTimeSum().equals("")) {
						anaDailyStat.setPlayTime(seriesStat.getPlayTimeSum() / day);
					} else {

						anaDailyStat.setPlayTime(0);
					}
					// 平均每天评论总次数
					if (0 != seriesStat.getCommentCount() && null != seriesStat.getCommentCount()
							&& !seriesStat.getCommentCount().equals("")) {
						anaDailyStat.setCommentCount(seriesStat.getCommentCount() / day);
					} else {

						anaDailyStat.setCommentCount(0);
					}
					// 平均每天点赞总数
					if (0 != seriesStat.getLikeCount() && null != seriesStat.getLikeCount()
							&& !seriesStat.getLikeCount().equals("")) {
						anaDailyStat.setLikeCount(seriesStat.getLikeCount() / day);
					} else {

						anaDailyStat.setLikeCount(0);

					}
					// 平均 每天分享数
					if (0 != seriesStat.getShareCount() && null != seriesStat.getShareCount()
							&& !seriesStat.getShareCount().equals("")) {
						anaDailyStat.setShareCount(seriesStat.getShareCount() / day);
					} else {
						anaDailyStat.setShareCount(0);

					}
					// 插入表示
					anaDailyStat.setMark(3);

				}

			}
		}

		return anaDailyStat;

	}

	@Override
	public AnaDailyStatEntity conditionCommentMax(String seriesName) {
		// TODO Auto-generated method stub

		SeriesStatEntity seriesStat = seriesStatMapper.conditionCommentAvg(seriesName);
		AnaDailyStatEntity anaDailyStat = new AnaDailyStatEntity();
		if (null != seriesStat) {
			// 历史播放次数峰值
			if (0 != seriesStat.getMaxPlayCount() && null != seriesStat.getMaxPlayCount()
					&& !seriesStat.getMaxPlayCount().equals("")) {
				anaDailyStat.setPlayCount(seriesStat.getMaxPlayCount());
			} else {
				anaDailyStat.setPlayCount(0);
			}

			// 历史播放时长峰值
			if (0 != seriesStat.getMaxPlayTime() && null != seriesStat.getMaxPlayTime()
					&& !seriesStat.getMaxPlayTime().equals("")) {
				anaDailyStat.setPlayTime(seriesStat.getMaxPlayTime());
			} else {
				anaDailyStat.setPlayTime(0);
			}

			// 历史评论峰值
			if (0 != seriesStat.getMaxCommentCount() && null != seriesStat.getMaxCommentCount()
					&& !seriesStat.getMaxCommentCount().equals("")) {
				anaDailyStat.setCommentCount(seriesStat.getMaxCommentCount());
			} else {
				anaDailyStat.setCommentCount(0);
			}
			// 历史点赞峰值
			if (0 != seriesStat.getMaxLikeCount() && null != seriesStat.getMaxLikeCount()
					&& !seriesStat.getMaxLikeCount().equals("")) {
				anaDailyStat.setLikeCount(seriesStat.getMaxLikeCount());
			} else {

				anaDailyStat.setLikeCount(0);
			}

			// 历史分享峰值
			if (0 != seriesStat.getMaxShareCount() && null != seriesStat.getMaxShareCount()
					&& !seriesStat.getMaxShareCount().equals("")) {
				anaDailyStat.setShareCount(seriesStat.getMaxShareCount());
			} else {

				anaDailyStat.setShareCount(0);
			}
			// 标示
			anaDailyStat.setMark(4);

		}

		return anaDailyStat;
	}

	// 获取频道类型信息
	@Override
	public List<SeriesStatEntity> channelList() {
		// TODO Auto-generated method stub
		List<SeriesStatEntity> seriesStatList = seriesStatMapper.channelList();
		if (null != seriesStatList) {
			for (SeriesStatEntity seriesStat : seriesStatList) {
				if (null != seriesStat.getChannel() && !seriesStat.getChannel().equals("")) {
					switch (seriesStat.getChannel()) {
					case "MOVIE":
						seriesStat.setChannel("电影");
						break;
					case "TVPLAY":
						seriesStat.setChannel("电视剧");
						break;
					case "CARTOON":
						seriesStat.setChannel("动漫");
						break;
					}
				}

			}
		}

		return seriesStatList;
	}

	// 根据传入的参数进行播放统计
	@Override
	public List<SeriesStatEntity> statPlaySum(Bdictionary bdictionary) {
		// TODO Auto-generated method stub
		List<SeriesStatEntity> seriesStatList = new ArrayList<SeriesStatEntity>();
		List<Bdictionary> dictionaryList = new ArrayList<Bdictionary>();
		if (null != bdictionary.getType() && !bdictionary.getType().equals("")) {
			dictionaryList = dictionaryMapper.dictionaryList(bdictionary.getType(), bdictionary.getCode());
		}
		if (null != dictionaryList) {
			// 全频道统计
			if (bdictionary.getType().equals(PlayStatisticsConfig.channel)) {
				for (Bdictionary dic : dictionaryList) {
					SeriesStatEntity ser = new SeriesStatEntity();
					if (null != ser) {
						ser.setChannel(dic.getName());

						ser.setPlayCount(seriesStatMapper.statPlaySumChannel(dic.getCode()));

						seriesStatList.add(ser);
					}
				}

			} else if (bdictionary.getType().equals(PlayStatisticsConfig.kind)) { // 根据剧集类型进行统计

				for (Bdictionary dic : dictionaryList) {
					SeriesStatEntity ser = new SeriesStatEntity();
					if (null != ser) {
						ser.setKindName(dic.getName());
						if (null != seriesStatMapper.statPlaySumKind(bdictionary.getCode(), dic.getCode())) {

							ser.setPlayCount(seriesStatMapper.statPlaySumKind(bdictionary.getCode(), dic.getCode()));
						} else {
							ser.setPlayCount(0);
						}
						seriesStatList.add(ser);
					}
				}

			} else if (bdictionary.getType().equals(PlayStatisticsConfig.type)) {// 根据剧情

				for (Bdictionary dic : dictionaryList) {
					SeriesStatEntity ser = new SeriesStatEntity();
					if (null != ser) {
						ser.setTypeName(dic.getName());
						if (null != seriesStatMapper.statPlaySumType(bdictionary.getCode(), dic.getCode())) {
							ser.setPlayCount(seriesStatMapper.statPlaySumType(bdictionary.getCode(), dic.getCode()));

						} else {
							ser.setPlayCount(0);
						}

						seriesStatList.add(ser);
					}
				}
			}
		}
		return seriesStatList;
	}

	// 查询所有剧集信息
	@Override
	public List<SeriesStatEntity> getSelectAllList() {
		// TODO Auto-generated method stub
		return seriesStatMapper.getSelectAllList();
	}
}
